package cn.m3a1.dao;

import cn.m3a1.model.Course;
import cn.m3a1.model.User;
import com.iceking.dynamicweb.DBConnection;
import com.iceking.dynamicweb.TaskProcessor;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wyj
 * @date 2019/5/24
 * @time 下午4:50
 */
public class UserDao {

    public List findallUser(DBConnection dbConnection) throws Exception {
        User user;
        String sql = "select * from user";
        //定义结果集来接受查询的数据
        ResultSet rs = dbConnection.query(sql);
        List<User> list = new ArrayList<>();
        while (rs.next()) {
            user = new User();
            user.setId(rs.getInt("id"));
            user.setType(rs.getInt("type"));
            user.setCode(rs.getString("code"));
            user.setUsername(rs.getString("username"));
            user.setName(rs.getString("name"));
            user.setEmail(rs.getString("email"));
            user.setTel(rs.getString("tel"));
            user.setPassword(rs.getString("password"));


            list.add(user);

        }
        return list;
    }

    public ArrayList<Course> findcourse(DBConnection dbConn, int id) throws Exception {
        User user;
        String sql = "select course.id, course.teacherid,course.`name` from studentofclass,course where studentofclass.classid=course.classid and studentofclass.studentid=" + id;
        //定义结果集来接受查询的数据
        ResultSet rs = dbConn.query(sql);
        ArrayList<Course> list = new ArrayList<>();
        while (rs.next()) {
            Course course = new Course();
            course.setTeacherid(rs.getInt("teacherid"));
            course.setName(rs.getString("name"));
            course.setId(rs.getInt("id"));
            list.add(course);

        }
        return list;
    }
}
